//
//  MainViewController.swift
//  sinaweibo
//
//  Created by 沈勇 on 16/5/1.
//  Copyright © 2016年 沈勇. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //不加@objc 修饰的话，私有方法不能外面的类中调用，所以会导致程序崩溃
    @objc private func composeDidClick() {
        
        print(__FUNCTION__)
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //由于 tabbar是只读属性， 不能直接操作，如果要修改，可以使用KVC
        let mainTabBar = MainTabBar()
        
        setValue(mainTabBar, forKey: "tabBar")
        
        //添加子视图控制器
        addChildViewControllers()
        
        //设置composeButton按钮的点击事件
        mainTabBar.composeButton.addTarget(self, action: "composeDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    //加载子视图控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName:"tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    //重载
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        //实例化导航视图控制器
        let nav = UINavigationController(rootViewController: vc)
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //添加
        addChildViewController(nav)
        
    }
   

}
